#include "LinkStack.h"
#include "LinkQueue.h"
#include <stdio.h>
#include <MyString.h>
#include <time.h>
#include <StdFile.h>

#define StopSize 3
#define CleanScanf while(getchar() != '\n')
#define PerPrice 5


MyStr GetTimeStr(const long t)
{
    struct tm * m = gmtime(&t);
    INITSTR(str);
    ResizeString(&str,100);
    sprintf(str.string,"|%d年%d月%d日 %d时%d分%d秒|",m->tm_year +1900,m->tm_mon +1,m->tm_mday,(m->tm_hour +8)%24,m->tm_min,m->tm_sec);
    return str;
}

//车
typedef struct
{
    MyStr name;
    long arriveTime;//汽车的到达时间
    long leaveTime;//汽车的离开时间
}Car;

void PrintCar(void *ptr)
{
    Car *c = (Car *)ptr;
    MyStr t = GetTimeStr(c->arriveTime);
    printf("车牌:|%s|,到达时间:%s\n",c->name.string,GetTimeStr(c->arriveTime).string);
}

Car *CreateCar(const char* name,long arriveTime)
{
    Car *c = (Car *)malloc(sizeof(Car));
    if(c == NULL)
    {
        printf("创建车失败！\n");
        return NULL;
    }
    c->name = GetString(name);
    c->arriveTime = arriveTime;
}

void freeCar(void *ptr)
{
    Car *c = (Car*)ptr;
    ClearString(&c->name);
    free(c);
}

void WriteCarToFile(Car *c)
{
    char fileContent[100] = {0};
    sprintf(fileContent,"%s %ld %ld %ld\n",c->name.string,c->arriveTime,c->leaveTime,(c->leaveTime - c->arriveTime) * PerPrice);
    AppendToFIle("carStop.txt",fileContent,strlen(fileContent));
}

//来车
void ComeCar(LStack *CarStop, LQueue *pavement)
{
    printf("请输入车牌号:");
    char carName[100] = {0};
    scanf("%s",carName);
    while (getchar() != '\n');

    long t = time(NULL);
    Car *newCar = CreateCar(carName,t);
    if(newCar == NULL)
        return;
    //停车场未满
    if(GetStackLen(CarStop) < StopSize)
    {
        SPush(CarStop,newCar);
        printf("当前停车场未满，车辆|%s|停在了停车场的第%d个位置\n",newCar->name.string,GetStackLen(CarStop));
    }
    //停车场满了
    else
    {
        QPush(pavement,newCar);
        printf("当前停车场已满，车辆|%s|停在了便道的第%d个位置\n",newCar->name.string,GetQueueLen(pavement));
    }
}

//车辆离开
void LeaveCar(LStack *carStop,LQueue *pavement)
{
    printf("请输入要离开的车辆:");
    char carName[100] = {0};
    scanf("%s",carName);
    while(getchar() != '\n');
    int flag = 0;

    LStack *tempS = InitStack();
    long leaveTime = 0;
    while(IsStackEmpty(carStop) == false)
    {
        Car *c = (Car*)*GetSTop(carStop);
        //找到了这辆车
        if(IsEqual(&c->name,carName) == true)
        {
            flag = 1;
            c->leaveTime = time(NULL);
            leaveTime = c->leaveTime;

            WriteCarToFile(c);

            printf("车辆|%s|在%s到达停车场，在%s离开停车场，总费用是|%ld|\n",
            c->name.string,GetTimeStr(c->arriveTime).string,GetTimeStr(c->leaveTime).string,(c->leaveTime - c->arriveTime) * PerPrice);

            SPop(carStop);
            //清除车辆
            freeCar(c);
            break;
        }
        //车辆进入临时栈，从停车场出栈
        SPush(tempS,c);
        SPop(carStop);
    }

    if(flag == 0)
    printf("没有找到！\n");
    while(IsStackEmpty(tempS) == false)
    {
        SPush(carStop,*GetSTop(tempS));
        SPop(tempS);
    }

    ClearLStack(tempS,NULL);


        //便道上是否有车
    if(GetStackLen(carStop) < StopSize && IsQueueEmpty(pavement) == false)
    {
        //便道上第一辆车出队列
        Car *frontC = (Car*)*GetQFront(pavement);
        QPop(pavement);

        frontC->arriveTime = leaveTime;
        printf("车辆|%s|从便道上进入停车场，时间是%s\n",frontC->name.string,GetTimeStr(frontC->arriveTime).string);
        //车辆进入停车场
        SPush(carStop,frontC);
    }
}

void DisPlayCarStop()
{
    MyStrList list = GetLineFromFile("carStop.txt");
    long totalMoney = 0;
    for(int i = 0; i < list.len; i++)
    {
        Car c = {NULL,0,0};
        ResizeString(&c.name,100);
        long money = 0;
        sscanf(list.stringList[i].string,"%s %ld %ld %ld",c.name.string,&c.arriveTime,&c.leaveTime,&money);
        printf("车辆|%s|在%s到达停车场，在%s离开停车场，总费用是|%ld|\n",c.name.string,GetTimeStr(c.arriveTime).string,GetTimeStr(c.leaveTime).string,money);
        totalMoney += money;
    }

    printf("总收入：￥%ld\n",totalMoney);
}


int main()
{
    //定义一个停车场
    LStack *carStop = InitStack();

    //定义一个便道
    LQueue *pavement = InitLQueue();
    
    if(IsFileExist("carStop.txt") == false)
    {
        CreateNewFile("carStop.txt");
    }

    while(1)
    {
        printf("====================停车场=====================\n");
        printf("1、车辆进入停车场\n");
        printf("2、车辆离开停车场\n");
        printf("3、显示停车场信息\n");
        printf("4、显示便道信息\n");
        printf("5、显示停车场出车流水\n");
        printf("6、退出\n");

        int choice = 0;
        scanf("%d",&choice);
        CleanScanf;

        switch(choice)
        {
            case 1:
                ComeCar(carStop,pavement);
                break;
            case 2:
                LeaveCar(carStop,pavement);
                break;
            case 3:
                TravelLStack(carStop,PrintCar);
                break;
            case 4:
                TravelLQueue(pavement,PrintCar);
            case 5:
                DisPlayCarStop();
                break;
            case 6:
                ClearLStack(carStop,freeCar);
                ClearLQueue(pavement,freeCar);
                exit(0);
            default :
                break;
        }
    }

    return 0;
}